﻿using System;
using System.Windows.Forms;
using Calendar.Domain;
using System.Drawing;

namespace Calendar.GUI
{
    public partial class TodoListForm : Form
    {
        private WorkHandler _workHandler;
        TreeNode _emptyWorkNode;

        public TodoListForm()
        {
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(100, 100);
            InitializeComponent();
            _workHandler = new WorkHandler();
            _workHandler.AddWorkEvent += AddWork;
            _workHandler.AddDateWorkEvent += AddDateWork;
            _workHandler.EmptyWorkEvent += AddEmptyWork;
            _emptyWorkNode = new TreeNode("無待辦事項");
            Initialize();
        }

        private void Initialize()
        {
            todoTreeView.Nodes.Clear();
            LoadWork();
            todoTreeView.ExpandAll();           
        }

        public void AddEmptyWork()
        {
            todoTreeView.Nodes.Add(_emptyWorkNode);
        }

        private void LoadWork()
        {
            _workHandler.LoadWork();
        }

        private void CreateWork(object sender, EventArgs e)
        {
            WorkForm createWorkForm = new WorkForm(_workHandler);
            createWorkForm.ShowDialog();
        }

        public void AddWork(int id, string name, DateTime deadline)
        {
            TreeNode workName = new TreeNode(name);
            workName.Tag = id;
            FindDateNode(_workHandler.FormatDate(deadline.ToShortDateString()), todoTreeView).Nodes.Add(workName);
            UpdateTreeNode(workName);
        }

        public void AddDateWork(int id, string name, DateTime deadline)
        {
            //todoTreeView.Nodes.Remove(_emptyWorkNode);
            _emptyWorkNode.Remove();
            TreeNode workName = new TreeNode(name);
            workName.Tag = id;
            TreeNode date = new TreeNode(_workHandler.FormatDate(deadline.ToShortDateString()), new TreeNode[] { workName });
            todoTreeView.Nodes.Add(date);
            UpdateTreeNode(workName);
            todoTreeView.Sort();
        }

        public TreeNode FindDateNode(string target, TreeView treeView)
        {
            foreach (TreeNode node in treeView.Nodes)
            {
                if (node.Text == target)
                {
                    return node;
                }
            }
            return null;
        }

        private void SelectWork(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode node = e.Node;
            if (node.Tag != null)//node.GetNodeCount(true) == 0 && (int)node.Tag != 0
            {
                WorkRecordForm workRecord = new WorkRecordForm((int)node.Tag, _workHandler);
                workRecord.StartPosition = FormStartPosition.Manual;
                workRecord.Location = new Point(400,100);
                workRecord.UpdateEvent += UpdateTreeNode;
                workRecord.ShowDialog();
            }
        }

        private void UpdateTreeNode()
        {
            foreach (TreeNode node in todoTreeView.Nodes)
            {
                foreach (TreeNode child in node.Nodes)
                {
                    UpdateTreeNode(child);
                }
            }
        }

        private void UpdateTreeNode(TreeNode node)
        {
            node.Text = _workHandler.GetName((int)node.Tag);
            //node.Parent.Text = _workHandler.GetDeadline((int)node.Tag).ToShortDateString();
            node.Checked = _workHandler.IsChecked((int)node.Tag);
            node.ForeColor = _workHandler.GetNodeColor((int)node.Tag);
            node.NodeFont = new Font(DefaultFont.Name, DefaultFont.Size, _workHandler.GetNodeFontStyle((int)node.Tag));
        }

        private void todoTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                _workHandler.SetProgress((int)e.Node.Tag, 100);
                e.Node.ForeColor = _workHandler.GetNodeColor((int)e.Node.Tag);
                e.Node.NodeFont = new Font(DefaultFont.Name, DefaultFont.Size, _workHandler.GetNodeFontStyle((int)e.Node.Tag));
            }
        }

        private void todoTreeView_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && todoTreeView.SelectedNode.Tag != null)
            {
                _workHandler.DeleteWork((int)todoTreeView.SelectedNode.Tag);
                todoTreeView.SelectedNode.Remove();
            }           
        }
    }
}
